//
//  UserLocationViewController.m
//  jingjing2.0
//
//  Created by apple on 16/4/29.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LocationCityViewController.h"

@interface LocationCityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation LocationCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   [self setUpNav];
    
   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
}
#pragma mark--进来和出去的一些设置
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
    //刷新数据
    if (!self.cityArr) {
      [self getExitedCity];
    }

}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
    
}
#pragma mark--设置导航
-(void)setUpNav
{
    self.view.backgroundColor=[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.title=@"选择城市";
    UIBarButtonItem *left=[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"nav_back"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                          style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem=left;
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark--获取开通的城市

-(void)getExitedCity
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSString *timeStamp=[GeneralTool gettimestamp];
    
    NSDictionary *signDic=@{@"Timestamp":timeStamp};
    
    NSString *sign=[GeneralTool GetSin:signDic];
    
    NSString *urlString=[NSString stringWithFormat:@"Common/OpenCity/%@/%@",timeStamp,sign];
    
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        if ([responses[@"IsSuccess"]intValue]==1) {
            
            self.cityArr=responses[@"ReturnObject"];
            [_tableView reloadData];
        }else
        {
            [MBProgressHUD showError:@"获取城市失败"];
        }
        
    }];

}

#pragma mark--表
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kFrame.size.width, kFrame.size.height-64) style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 1;
    }else
    {
        return self.cityArr.count;
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section==0)
    {
        return @"当前定位城市";
    }else
    {
        return @"已开通城市";
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.section==0)
    {
        cell.textLabel.text=self.currentCity;
        for (NSDictionary *dic in self.cityArr) {
            NSString *name=dic[@"CityName"];
            if ([name isEqualToString:self.currentCity]) {
                cell.tag=[dic[@"CityCode"]integerValue];
            }
        }
    }else
    {
      NSDictionary *dic=self.cityArr[indexPath.row];
      cell.textLabel.text=dic[@"CityName"];
      cell.tag=[dic[@"CityCode"]integerValue];
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[_tableView cellForRowAtIndexPath:indexPath];
    NSString *cityCode=[NSString stringWithFormat:@"%zd",cell.tag];
    
    NSUserDefaults *data=[NSUserDefaults standardUserDefaults];
    [data setObject:cell.textLabel.text forKey:@"CurrentCity"];
    [data synchronize];

    NSUserDefaults *dataCode=[NSUserDefaults standardUserDefaults];
    [dataCode setObject:cityCode forKey:@"cityCode"];
    [dataCode synchronize];
    _passCityBlock(cell.textLabel.text,cityCode);

    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
